
/**   
*    
* 项目名称：a_yongan   
* 类名称：GoodTypeServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月18日 下午2:10:43    
* 修改时间：2018年6月18日 下午2:10:43   
* 修改备注：   
* @version    
*    
*/
package com.yongan.production.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.yongan.production.dao.ProductionPlanDao;
import com.yongan.production.entity.Details;
import com.yongan.production.entity.ProductionPlan;
import com.yongan.production.service.ProductionPlanService;
import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodsinformationDao;
import com.yongan.warehouse.entity.Goodsinformation;
import com.yongan.warehouse.service.GoodsinformationService;

/**
 * @author liubo
 *
 */
@Service
public class ProductionPlanServiceImpl implements ProductionPlanService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ProductionPlanDao productionPlanDao;
	@Autowired
	private GoodsinformationService goodsinformationService;
	@Autowired
	private GoodsinformationDao goodsinformationDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<ProductionPlan> getProductionPlan(int offset, int limit) {
		List<ProductionPlan> list = productionPlanDao.queryAll(offset, limit);
		for(ProductionPlan pp:list){
			String id = pp.getProductionPlangoodsId();
			Goodsinformation gi = goodsinformationService.getGoodsinformationById(id);
			pp.setGoodsinformation(gi);
		}
		return list;
	}

	
	@Override
	public ProductionPlan getProductionPlanById(String productionPlanId) {
		ProductionPlan productionPlan = productionPlanDao.queryById(productionPlanId);
		Goodsinformation gi = goodsinformationService.getGoodsinformationById(productionPlan.getProductionPlangoodsId());
		productionPlan.setGoodsinformation(gi);
		return productionPlan;
		
	}

	
	@Override
	public Execution executeProductionPlan(ProductionPlan productionPlan, String productionPlanId,
			String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(productionPlanId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(productionPlan == null){
				int deleteCount = productionPlanDao.deleteProductionPlan(productionPlanId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(productionPlanId, UserStatEnum.SUCCESS);
				}
			}else if((productionPlanId.equals(""))){
				int addCount = productionPlanDao.insertProductionPlan(productionPlan);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(productionPlan.getProductionPlanId(), UserStatEnum.SUCCESS);
				}
			}else if(!(productionPlanId.equals(""))){
				int updateCount = productionPlanDao.updateProductionPlan(productionPlan);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(productionPlanId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	public int getCount() {
		
		return productionPlanDao.queryCount();
	}
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}

	@Transactional
	@Override
	public Execution executeLingLiao(String productionPlanId,List<Details> detailsall,
			String md5) throws UserException {
		ProductionPlan pp = productionPlanDao.queryById(productionPlanId);
		String shuliang = pp.getProductionPlanSl();
		try{
			Boolean is = false;
			if(detailsall!=null){
				for(Details dt:detailsall){
					int jie = reduceNumber(dt, "reduce",shuliang);
					if(jie<=0){
						is = true;
					}
				}
				int zhu = productionPlanDao.updateLingLiao("true", productionPlanId);
				if(!is & zhu<=0){
					throw new UserException("lingliao failed");
				}else{
					return new Execution("", UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("lingliao failed");
			}
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}
	public int reduceNumber(Details igd,  String type,String shuliang){
		Integer zonghe = (Integer.parseInt(shuliang))*(Integer.parseInt(igd.getDetailsSl()));
		System.out.println(shuliang+"========"+String.valueOf(zonghe));
		return goodsinformationDao.reduceNumber(igd.getGoodsinformation().getGoodsinformationNumber()
				, type, zonghe
				, zonghe);
	}
}
